package net.j4love.es.example;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author he peng
 * @create 2017/11/24 20:33
 * @see
 */
public class ElasticsearchQuickStart {

    public static void main(String[] args) throws Exception {

        // on startup

        Settings settings = Settings.builder()
                .put("cluster.name", "my-application").build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(
                        new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        // object
        Map<String, Object> map = new HashMap<>(3);
        map.put("user","kimchy");
        map.put("postDate",new Date());
        map.put("message","trying out Elasticsearch");

        // use jackson build json document

        // 创建 index - twitter , type - tweet , id - 1 , 保存文档
        IndexResponse indexResponse =
                    client.prepareIndex("twitter", "tweet", "1")
                            .setSource(map).get();

        System.err.println("index response : " + indexResponse);
        // on shutdown

        client.close();

    }
}
